<div class="uc_title m_10 tabs_menu">
	<label class="current node"><span>用户头像修改</span></label>
</div>
<table class="form_table" width="100%" cellpadding="0" cellspacing="0">
	<form enctype="multipart/form-data" id="upavatar">
		<col width="120px" />
		<col />
		<tr>
			<input type="hidden" name="opr" value="update">
			<input type="hidden" name="type" value="2">
			<th>请选择头像：</th>
			<td><input type="file" id="avatar" name="avatar" /><label></label></td>
		</tr>
		<tr>
			<th></th>
			<td><label class="btn"><input type="button" id="save" value="保存" /></label> <label class="btn"><input
						type="button" id="cancel" value="取消" /></label></td>
		</tr>
	</form>
</table>
<script>

	var uploadFile;
	$(function () {
		$('#avatar').on('change', function () {
			var files = this.files;
			if (files.length == 0) {
				return;
			}
			uploadFile = files[0];
		});
		$('#save').on('click', function () {
			var formData = new FormData();
			formData.append("file", uploadFile); // 后端通过 'file' 获取
			$.ajax({
				url: '/user?opr=update&type=2',
				data: formData,
				type: "post",
				dataType: "json",
				processData: false, //必须false才会避开jQuery对 formdata 的默认处理 XMLHttpRequest会对 formdata 进行正确的处理
				contentType: false, //必须false才会自动加上正确的Content-Type
				beforeSend: function (r) {
					//加载层-默认风格
					layer.load();
				},
				success: function (r, states, xh) {
					if (r.msg == 'ok') {
						layer.msg("头像修改成功", { icon: 1 });
					} else {
						layer.msg("<em style='color:red'>" + r.msg + "</em>", { time: 1200, icon: 5 });
					}
				},
				complete: function (XMLHttpRequest, textStatus) {
					// alert('远程调用成功，状态文本值：'+textStatus);
					layer.closeAll('loading');
				},
				error: function () {
					layer.msg("系统繁忙，请稍后再试", { icon: 2 });
				}
			});
		});
	});


</script>